﻿Imports System.Data
Imports ADOEntityETL
Imports ADOEntity

Partial Class Form_frmRptDailyETL
    Inherits System.Web.UI.Page

    'Private ds As New dsDailyETLSync

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strdeptCode As String
        Dim strBranchCode As String
        Dim struserName As String
        Dim strETL_STATUS As String
        Dim strETL_ACCT_STATUS As String
        Dim strCUST_COMPLETE_FLAG As String
        Dim strKYCCOMPLETE As String
        Dim strStartDate As String
        Dim strEndDate As String
        Dim strUniqueID As String
        Dim strSECTION As String
        Dim strEmpCode As String
        Dim strEmpName As String

        If Not (IsPostBack) Then
            hdfVar1.Value = Request.Item("deptCode")
            hdfVar2.Value = Request.Item("BranchCode")
            hdfVar3.Value = Request.Item("userName")
            hdfVar4.Value = Request.Item("ETL_STATUS")
            hdfVar5.Value = Request.Item("ETL_ACCT_STATUS")
            hdfVar6.Value = Request.Item("CUST_COMPLETE_FLAG")
            hdfVar7.Value = Request.Item("KYCCOMPLETE")
            hdfVar8.Value = Request.Item("StartDate")
            hdfVar9.Value = Request.Item("EndDate") 
            hdfVar10.Value = Request.Item("UniqueID")
            hdfVar11.Value = Request.Item("SECTION")
            hdfVar12.Value = Request.Item("EmpCode")
            hdfVar13.Value = Request.Item("EmpName")

            strdeptCode = Request.Item("deptCode")
            strBranchCode = Request.Item("BranchCode")
            struserName = Request.Item("userName")
            strETL_STATUS = Request.Item("ETL_STATUS")
            strETL_ACCT_STATUS = Request.Item("ETL_ACCT_STATUS")
            strCUST_COMPLETE_FLAG = Request.Item("CUST_COMPLETE_FLAG")
            strKYCCOMPLETE = Request.Item("KYCCOMPLETE")
            strStartDate = Request.Item("StartDate")
            strEndDate = Request.Item("EndDate")
            strUniqueID = Request.Item("UniqueID")
            strSECTION = Request.Item("SECTION")
            strEmpCode = Request.Item("EmpCode")
            strEmpName = Request.Item("EmpName")
        Else
            If Not (IsCallback) Then
                strdeptCode = convertStringEncode(hdfVar1.Value)
                strBranchCode = convertStringEncode(hdfVar2.Value)
                struserName = convertStringEncode(hdfVar3.Value)
                strETL_STATUS = convertStringEncode(hdfVar4.Value)
                strETL_ACCT_STATUS = convertStringEncode(hdfVar5.Value)
                strCUST_COMPLETE_FLAG = convertStringEncode(hdfVar6.Value)
                strKYCCOMPLETE = convertStringEncode(hdfVar7.Value)
                strStartDate = convertStringEncode(hdfVar8.Value)
                strEndDate = convertStringEncode(hdfVar9.Value)
                strUniqueID = convertStringEncode(hdfVar10.Value)
                strSECTION = convertStringEncode(hdfVar11.Value)
                strEmpCode = convertStringEncode(hdfVar12.Value)
                strEmpName = convertStringEncode(hdfVar13.Value)
            Else
                strdeptCode = hdfVar1.Value
                strBranchCode = hdfVar2.Value
                struserName = hdfVar3.Value
                strETL_STATUS = hdfVar4.Value
                strETL_ACCT_STATUS = hdfVar5.Value
                strCUST_COMPLETE_FLAG = hdfVar6.Value
                strKYCCOMPLETE = hdfVar7.Value
                strStartDate = hdfVar8.Value
                strEndDate = hdfVar9.Value
                strUniqueID = hdfVar10.Value
                strSECTION = hdfVar11.Value
                strEmpCode = hdfVar12.Value
                strEmpName = hdfVar13.Value
            End If

        End If
        dailyEtl(strdeptCode, strBranchCode, struserName, strETL_STATUS, strETL_ACCT_STATUS, strCUST_COMPLETE_FLAG, strKYCCOMPLETE, strStartDate, strEndDate, strUniqueID, strSECTION, strEmpCode, strEmpName)

    End Sub

    Private Function convertStringEncode(ByVal strCon As String) As String
        Dim rtStr As String = ""
        Try

            rtStr = Encoding.UTF8.GetString(Encoding.GetEncoding("tis-620").GetBytes(strCon))
        Catch ex As Exception
            Throw ex
        End Try
        Return rtStr
    End Function



    Private Sub dailyEtl(ByVal strDeptCode As String, ByVal strBranchCode As String, _
                         ByVal strUserName As String, ByVal strETL_STATUS As String, _
                            ByVal strETL_ACCT_STATUS As String, ByVal strCUST_COMPLETE_FLAG As String, ByVal strKYCCOMPLETE As String, _
                         ByVal strStartDate As String, ByVal strEndDate As String, ByVal strUniqueID As String, ByVal strSECTION As String, ByVal strEmpCode As String, ByVal strEmpName As String)


        Dim clsVarParameter As clsVariable
        Dim clsSessionMenuRight As New clsSessionMenuRight
        Dim clsFormatDate As New clsDateFormat
        Dim colVarParameter As Collection = New Collection
        Dim StrWhere As String = ""
        Dim strConcat As String = " where "
        Dim strPrmCondition As String = ""
        Dim strSql As String = ""
        Dim arrDepCode As String() = Nothing
        Dim arrBranchCode As String() = Nothing
        Dim arrStatus As String() = Nothing


        Dim ds As New dsDailyETLSync
        Dim cls As New clsConnToDbByReport
        Dim re As rptDailyETLSync = New rptDailyETLSync





        If Not strDeptCode = "" Then
            arrDepCode = strDeptCode.Split("/")
            If Not arrDepCode(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.DEPTCODE = :DEPTCODE", StrWhere, strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "DEPTCODE", _
                                                          .valueColumn = arrDepCode(0)}
                colVarParameter.Add(clsVarParameter)


                re.prmDep.Value = arrDepCode(1)
            Else
                re.prmDep.Value = " ทั้งหมด "
            End If
        End If

        If Not strBranchCode = "" Then
            arrBranchCode = strBranchCode.Split("/")
            If Not arrBranchCode(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.BRANCHCODE = :BRANCHCODE", _
                                         StrWhere, _
                                         strConcat)
                strConcat = " And "
                clsVarParameter = New clsVariable() With {.nameColumn = "BRANCHCODE", _
                                                          .valueColumn = arrBranchCode(0)}
                colVarParameter.Add(clsVarParameter)

                re.prmBranch.Value = arrBranchCode(1)
            Else
                re.prmBranch.Value = " ทั้งหมด "

            End If
        End If

        If Not strUserName = "" Then
            If (strUserName.Length < 5) Then

                StrWhere = String.Format("{0}{1}ur.USERNAME like '%' || :USERNAME || '%'", _
                                              StrWhere, _
                                              strConcat)
            Else

                StrWhere = String.Format("{0}{1}ur.USERNAME = :USERNAME", _
                                                   StrWhere, _
                                                   strConcat)
            End If

            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "USERNAME", _
                                                      .valueColumn = strUserName}
            colVarParameter.Add(clsVarParameter)
            re.prmUserId.Value = strUserName

        Else
            re.prmUserId.Value = " ทั้งหมด "
        End If


        If Not strUniqueID = "" Then
            StrWhere = String.Format("{0}{1}ds.UNIQUEID = '" & strUniqueID & "'", StrWhere, strConcat)
            strConcat = " And "

        End If


        If Not strCUST_COMPLETE_FLAG = "A" Then
            StrWhere = String.Format("{0}{1}ds.CUST_COMPLETE_FLAG = '" & strCUST_COMPLETE_FLAG & "'", StrWhere, strConcat)
            strConcat = " And "

            If strCUST_COMPLETE_FLAG = "Y" Then
                re.prmCUST_COMPLETE_FLAG.Value = " สมบูรณ์ "
            Else
                re.prmCUST_COMPLETE_FLAG.Value = " ไม่สมบูรณ์ "
            End If
        Else
            re.prmCUST_COMPLETE_FLAG.Value = " ทั้งหมด "
        End If

        If Not strKYCCOMPLETE = "A" Then
            StrWhere = String.Format("{0}{1}ds.KYCCOMPLETE = '" & strKYCCOMPLETE & "'", StrWhere, strConcat)
            strConcat = " And "
            If strKYCCOMPLETE = "Y" Then
                re.prmKYCCOMPLETE.Value = " สมบูรณ์ "
            Else
                re.prmKYCCOMPLETE.Value = " ไม่สมบูรณ์ "
            End If
        Else
            re.prmKYCCOMPLETE.Value = " ทั้งหมด "
        End If


        If Not strETL_STATUS = "" Then
            arrStatus = strETL_STATUS.Split("/")

            If Not arrStatus(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.ETL_STATUS =  :ETL_STATUS ", _
                                    StrWhere, _
                                    strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "ETL_STATUS", _
                                                          .valueColumn = arrStatus(0)}
                colVarParameter.Add(clsVarParameter)
                re.prmETL_STATUS.Value = arrStatus(0) & " : " & arrStatus(1)

            Else
                re.prmETL_STATUS.Value = " ทั้งหมด "

            End If

        End If

        If Not strSECTION = "" Then
            arrStatus = strSECTION.Split("/")

            If Not arrStatus(0) = "A" Then
                StrWhere = String.Format("{0}{1}ds.DEPUPDATE =  :DEPUPDATE ", _
                                    StrWhere, _
                                    strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "DEPUPDATE", _
                                                          .valueColumn = arrStatus(0)}
                colVarParameter.Add(clsVarParameter)
                re.prmSECTION.Value = arrStatus(0) & " : " & arrStatus(1)

            Else
                re.prmSECTION.Value = " ทั้งหมด "

            End If

        End If

        If Not strETL_ACCT_STATUS = "" Then
            arrStatus = strETL_ACCT_STATUS.Split("/")

            If Not arrStatus(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.ETL_ACCT_STATUS =  :ETL_ACCT_STATUS ", _
                                    StrWhere, _
                                    strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "ETL_ACCT_STATUS", _
                                                          .valueColumn = arrStatus(0)}
                colVarParameter.Add(clsVarParameter)
                re.prmETL_ACCT_STATUS.Value = arrStatus(0) & " : " & arrStatus(1)

            Else
                re.prmETL_ACCT_STATUS.Value = " ทั้งหมด "

            End If

        End If

        If Not (strStartDate = "" And strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            StrWhere = String.Format("{0}{1}ds.ETL_UPDATEDATE >= :StartDate and ds.ETL_UPDATEDATE <= :EndDate", _
                                     StrWhere, _
                                     strConcat)
            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "StartDate", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "EndDate", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)
            re.prmStartDate.Value = dteStartDate
            re.prmEndDate.Value = dteEndDate
        Else
            re.prmStartDate.Value = " - "
            re.prmEndDate.Value = " - "
        End If

        re.prmEmpCode.Value = strEmpCode
        re.prmEmpName.Value = strEmpName

        strSql = "select  ds.Custcode,ds.UNIQUEID,ds.THFIRSTNAME, ds.THMIDNAME, ds.THLASTNAME, " _
                & "        ds.ENFIRSTNAME, ds.ENMIDNAME, ds.ENLASTNAME,  ds.ACCOUNTNO, ds.ETL_ACCT_STATUS,ds.KYCCOMPLETE,  " _
                & "        ds.UPDATEBY, ds.DEPD,ds.DEPL,ds.DEPP,ds.DEPN, ds.ETL_UPDATEDATE,  ds.ETL_STATUS, ds.CUST_COMPLETE_FLAG, " _
                & "          ds.DEPTCODE, ds.ZONECODE, ds.BRANCHCODE, ds.TRANTYPE,ur.ROLECODE " _
                & " from " & System.Configuration.ConfigurationSettings.AppSettings("TableV_DAILYKYCETL") & "  ds   " _
                & "			  join " & System.Configuration.ConfigurationSettings.AppSettings("TableSUSER") & " ur on ds.UPDATEBY = ur.USERNAME " _
                & StrWhere _
                & " order by  ds.DEPTCODE  , ds.BRANCHCODE  , ds.UPDATEBY  , ds.ACCOUNTNO "



        cls.rptDailyETLSync(re, strSql, ds, colVarParameter)
        ' re.prmWhere.Value = strPrmCondition
        ReportViewer1.Report = re

    End Sub

    'Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    '    Session("ds") = ds
    'End Sub
End Class
